//
//  MOLoginViewController.m
//  MobileOffice
//
//  Created by 冰冰 on 2019/5/30.
//  Copyright © 2019 com.idolmedia. All rights reserved.
//

#import "MOLoginViewController.h"
#import "MOForgotPasswordViewController.h"
#import "MONavigationController.h"
#import "MOUser.h"
#import "MORegistViewController.h"

@interface MOLoginViewController ()

@property(nonatomic,strong)UITextField *userNameTextField;

@property(nonatomic,strong)UITextField *passTextField;

@property(nonatomic,strong)UITextField *codeTextField;

@property(nonatomic,strong)UITextField *testTextField;

@property(nonatomic,strong)UITextField *testTTextField;

@property(nonatomic,strong)UIButton *codeButton;

@end

@implementation MOLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self creatView];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(registSucess:) name:@"registSucess" object:nil];
    
    //sendCodeAgain
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(sendCodeAgain) name:@"sendCodeAgain" object:nil];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    NSString *outName = [[NSUserDefaults standardUserDefaults]objectForKey:@"outName"];
    if (outName.length>0) {
        self.userNameTextField.text = outName;
    }
}

-(void)registSucess:(NSNotification *)noti
{
    NSDictionary *dic = noti.userInfo;
    self.userNameTextField.text = dic[@"userName"];
}

-(void)creatView
{
    //背景视图
    UIImageView *backImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    backImageView.image = IMAGE(@"login_back");
    backImageView.userInteractionEnabled = YES;
    [self.view addSubview:backImageView];
    
    
    UILabel *showLabel = [[UILabel alloc]initWithFrame:CGRectMake(35, StatusBar_HEIGHT+kHConversions(74), SCREEN_WIDTH-70, 23)];
    showLabel.text = @"欢迎使用税务移动办公";//欢迎使用新疆税务移动办公
    if (@available(iOS 8.2, *)) {
        showLabel.font = [UIFont systemFontOfSize:23 weight:UIFontWeightMedium];
    } else {
        showLabel.font = [UIFont boldSystemFontOfSize:23];
    }
    showLabel.textColor = [UIColor whiteColor];
    [backImageView addSubview:showLabel];
    
    UILabel *userNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(35, showLabel.bottom+32, SCREEN_WIDTH-70, 12)];
    userNameLabel.text = @"系统用户名";
    userNameLabel.textColor = [UIColor whiteColor];
    userNameLabel.font = FONT_SIZE(13);
    [backImageView addSubview:userNameLabel];
    
    self.userNameTextField = [[UITextField alloc]initWithFrame:CGRectMake(35, userNameLabel.bottom+11, SCREEN_WIDTH-70, 15)];
    self.userNameTextField.font = FONT_SIZE(15);
    self.userNameTextField.placeholder = @"请输入系统用户名";
    [self.userNameTextField setValue:[[UIColor whiteColor]colorWithAlphaComponent:0.15] forKeyPath:@"_placeholderLabel.textColor"];
    self.userNameTextField.textColor = KWhiteColor;
    [backImageView addSubview:self.userNameTextField];
    
    
    UIView *line1 = [[UIView alloc]initWithFrame:CGRectMake(35, self.userNameTextField.bottom+10, SCREEN_WIDTH-70, 1)];
    line1.backgroundColor = [[UIColor whiteColor]colorWithAlphaComponent:0.15];
    [backImageView addSubview:line1];
    
    UILabel *passLabel = [[UILabel alloc]initWithFrame:CGRectMake(35, line1.bottom+21, SCREEN_WIDTH-70, 12)];
    passLabel.text = @"密码";
    passLabel.textColor = [UIColor whiteColor];
    passLabel.font = FONT_SIZE(13);
    [backImageView addSubview:passLabel];
    
    self.passTextField = [[UITextField alloc]initWithFrame:CGRectMake(35, passLabel.bottom+11, SCREEN_WIDTH-70, 15)];
    self.passTextField.font = FONT_SIZE(15);
    self.passTextField.placeholder = @"请输入密码";
    self.passTextField.secureTextEntry = YES;
    [self.passTextField setValue:[[UIColor whiteColor]colorWithAlphaComponent:0.15] forKeyPath:@"_placeholderLabel.textColor"];
    self.passTextField.textColor = KWhiteColor;
    [backImageView addSubview:self.passTextField];
    
    UIView *line2 = [[UIView alloc]initWithFrame:CGRectMake(35, self.passTextField.bottom+10, SCREEN_WIDTH-70, 1)];
    line2.backgroundColor = [[UIColor whiteColor]colorWithAlphaComponent:0.15];
    [backImageView addSubview:line2];
    
    UILabel *codeLabel = [[UILabel alloc]initWithFrame:CGRectMake(35, line2.bottom+21, SCREEN_WIDTH-70, 12)];
    codeLabel.text = @"验证码";
    codeLabel.textColor = [UIColor whiteColor];
    codeLabel.font = FONT_SIZE(13);
    [backImageView addSubview:codeLabel];
    
    self.codeTextField = [[UITextField alloc]initWithFrame:CGRectMake(35, codeLabel.bottom+11, SCREEN_WIDTH-70 - 83, 15)];
    self.codeTextField.font = FONT_SIZE(15);
    self.codeTextField.placeholder = @"请输入验证码";
    [self.codeTextField setValue:[[UIColor whiteColor]colorWithAlphaComponent:0.15] forKeyPath:@"_placeholderLabel.textColor"];
    self.codeTextField.textColor = KWhiteColor;
    [backImageView addSubview:self.codeTextField];
    
    UIView *line3 = [[UIView alloc]initWithFrame:CGRectMake(35, self.codeTextField.bottom+10, SCREEN_WIDTH-70, 1)];
    line3.backgroundColor = [[UIColor whiteColor]colorWithAlphaComponent:0.15];
    [backImageView addSubview:line3];
    
    
    UIButton *codeButton = [[UIButton alloc] initWithFrame:CGRectMake(CGRectGetMaxX(line3.frame) - 95, 0, 95, 12)];
    codeButton.centerY = self.codeTextField.centerY;
    [codeButton setTitle:@"获取短信验证码" forState:UIControlStateNormal];
    codeButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [codeButton setTitleColor:KWhiteColor forState:UIControlStateNormal];
    codeButton.titleLabel.font = FONT_SIZE(13);
    [codeButton addTarget:self action:@selector(clickCodeButton:) forControlEvents:UIControlEventTouchUpInside];
    [backImageView addSubview:codeButton];
    self.codeButton = codeButton;
    
    
    UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(35, line3.bottom + 30, SCREEN_WIDTH-70, 45)];
    [button setTitle:@"登录" forState:UIControlStateNormal];
    [button setBackgroundColor:KBlueColor_2C94FF];
    button.titleLabel.font = FONT_SIZE(15);
    button.layer.masksToBounds = YES;
    button.layer.cornerRadius = 3.0;
    [button addTarget:self action:@selector(clickButton) forControlEvents:UIControlEventTouchUpInside];
    [backImageView addSubview:button];
    
    UIButton *resignPasswordButton = [[UIButton alloc]initWithFrame:CGRectMake(35, button.bottom + 20, (SCREEN_WIDTH-70)/2, 12)];
    [resignPasswordButton setTitle:@"忘记密码" forState:UIControlStateNormal];
    resignPasswordButton.titleLabel.font = FONT_SIZE(13);
    [resignPasswordButton setContentHorizontalAlignment:(UIControlContentHorizontalAlignmentLeft)];
    [resignPasswordButton addTarget:self action:@selector(clickResignPasswordButton) forControlEvents:UIControlEventTouchUpInside];
    [backImageView addSubview:resignPasswordButton];
    
    UIButton *registButton = [[UIButton alloc]initWithFrame:CGRectMake(resignPasswordButton.right, button.bottom + 20, resignPasswordButton.width, 12)];
    [registButton setTitle:@"新用户注册" forState:UIControlStateNormal];
    registButton.titleLabel.font = FONT_SIZE(13);
    [registButton setContentHorizontalAlignment:(UIControlContentHorizontalAlignmentRight)];
    [registButton addTarget:self action:@selector(clickRegistButton) forControlEvents:UIControlEventTouchUpInside];
    [backImageView addSubview:registButton];
    
}



#pragma mark -点击事件登录
-(void)clickButton
{
    if (self.userNameTextField.text.length==0) {
        [[ADShowMessage shareShowMessage] showTextOnly:@"请输入用户名" showInView:self.view];
        return;
    }
    if (self.passTextField.text.length==0) {
        [[ADShowMessage shareShowMessage] showTextOnly:@"请输入密码" showInView:self.view];
        return;
    }
    if (self.codeTextField.text.length==0) {
        [[ADShowMessage shareShowMessage] showTextOnly:@"请输入验证码" showInView:self.view];
        return;
    }
    
    NSDictionary *user = @{
                              @"userName":self.userNameTextField.text,
                              @"password":[self sha1:[NSString stringWithFormat:@"%@%@",self.passTextField.text,self.userNameTextField.text]]
                              };
    
    NSString *postUrl = [NSString stringWithFormat:@"%@/%@",A_IN_URL,self.codeTextField.text];
    
//    [[ADShowMessage shareShowMessage] showHUDAddedTo:self.view animated:YES];
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    [PPNetworkHelper POST:postUrl parameters:user success:^(id responseObject) {
        NSLog(@"登录:%@",responseObject);
        
//        [[ADShowMessage shareShowMessage] hideHUDForView:self.view animated:YES];
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        
        if ([responseObject[@"code"] integerValue]==1) {//1 登录成功 0 登录失败
            [MOUser initUserWithDict:responseObject];
            AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
            [appDelegate creatTabbar];//创建tabbar
        }else{
            [[ADShowMessage shareShowMessage] showTextOnly:responseObject[@"msg"] showInView:WINDOW];
        }
    } failure:^(NSError *error) {
//        [[ADShowMessage shareShowMessage] hideHUDForView:self.view animated:YES];
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        
        [[ADShowMessage shareShowMessage] showTextOnly:[error localizedDescription] showInView:WINDOW];
    }];
    
    
}

-(void)sendCodeAgain
{
    [self performSelector:@selector(getTelCode) withObject:nil afterDelay:3];
}

#pragma mark -获取验证码
- (void)clickCodeButton:(UIButton *)sender{
    
    if (self.userNameTextField.text.length==0) {
        [[ADShowMessage shareShowMessage] showTextOnly:@"请输入用户名" showInView:self.view];
        return;
    }
    
    [sender SG_countdownWithSec:60];
    [self getTelCode];
}

-(void)getTelCode
{
    
    NSString *stand = [[NSUserDefaults standardUserDefaults]objectForKey:@"vpnLogin"];
    if ([stand integerValue]==0) {
       [[NSUserDefaults standardUserDefaults]setObject:@"1" forKey:@"sendCodeField"];
        return;
    }
        
    
    NSString *postUrl = [NSString stringWithFormat:@"%@/%@/%@",A_CAPTCHA_URL,@"LOGIN",self.userNameTextField.text];
    
    [PPNetworkHelper GET:postUrl parameters:@{} success:^(id responseObject) {
        NSLog(@"获取验证码:%@",responseObject);
        
        if ([responseObject integerValue]==0) {
            [[ADShowMessage shareShowMessage] showTextOnly:@"发送成功" showInView:self.view];
        }else if ([responseObject integerValue]==1){
            [[ADShowMessage shareShowMessage] showTextOnly:@"发送异常" showInView:self.view];
        }else if ([responseObject integerValue]==2){
            [[ADShowMessage shareShowMessage] showTextOnly:@"用户不存在" showInView:self.view];
        }else if ([responseObject integerValue]==3){
            [[ADShowMessage shareShowMessage] showTextOnly:@"用户为注册手机号码" showInView:self.view];
        }else{
            [[ADShowMessage shareShowMessage] showTextOnly:@"短信接口调用异常" showInView:self.view];
        }
        
    } failure:^(NSError *error) {
//        [self.codeButton SG_countdownWithSec:60];
        [[ADShowMessage shareShowMessage] showTextOnly:[error localizedDescription] showInView:self.view];
    }];
}


#pragma mark -忘记密码
-(void)clickResignPasswordButton
{
    MOForgotPasswordViewController *forgotPasswordVC = [[MOForgotPasswordViewController alloc] init];
    MONavigationController *nav = [[MONavigationController alloc] initWithRootViewController:forgotPasswordVC];
    [self presentViewController:nav animated:YES completion:nil];
}

#pragma mark -- 新用户注册
-(void)clickRegistButton
{
    MORegistViewController *vc = [[MORegistViewController alloc]init];
    MONavigationController *nav = [[MONavigationController alloc] initWithRootViewController:vc];
    [self presentViewController:nav animated:YES completion:nil];
}

#pragma mark -//sha1加密方式
- (NSString *) sha1:(NSString *)input
{
    //这两句容易造成 、中文字符串转data时会造成数据丢失
    //const char *cstr = [input cStringUsingEncoding:NSUTF8StringEncoding];
    //NSData *data = [NSData dataWithBytes:cstr length:input.length];
    //instead of
    NSData *data = [input dataUsingEncoding:NSUTF8StringEncoding];
    
    uint8_t digest[CC_SHA1_DIGEST_LENGTH];
    
    CC_SHA1(data.bytes, (unsigned int)data.length, digest);
    
    NSMutableString *output = [NSMutableString stringWithCapacity:CC_SHA1_DIGEST_LENGTH * 2];
    
    for(int i=0; i<CC_SHA1_DIGEST_LENGTH; i++) {
        [output appendFormat:@"%02x", digest[i]];
    }
    return output;
}



@end
